<?php

class Util_Devel {
	
	static public function sql($file,$db){
		$x = Zend_Db::factory('PDO_MYSQL', array(
		    'host'     => $db['host'],
	    	'username' => $db['user'],
		    'password' => $db['pass'],
		    'dbname'   => $db["db"]
		));
		$x->query('SET CHARACTER SET UTF8')->execute();	
		$schemaSql = file_get_contents(dirname(__FILE__) . '/SQL/' . $file);
		echo $x->getConnection()->exec($schemaSql);
	}
	
	static public function sqlOnTarget($file,$host,$db,$login,$pass){
		$x = Zend_Db::factory('PDO_MYSQL', array(
		    'host'     => $host,
	    	'username' => $login,
		    'password' => $pass,
		    'dbname'   => $db
		));
		$x->query('SET CHARACTER SET UTF8')->execute();	
		$schemaSql = file_get_contents(dirname(__FILE__) . '/SQL/' . $file);
		echo $x->getConnection()->exec($schemaSql);
	}
	
	
	/**
	 * Webservices - TESTing
	 */	
	static public function soapClient(){
		
		$host = $_SERVER['HTTP_HOST'];
		$wsdl = "http://$host/soap?wsdl";
		$client = new Zend_Soap_Client($wsdl);
		//UZI::auth($client);
		try {
			$r = $client->manageAC('144440001',false,'b666af28a40669989a7ec5dacd34ff36');
			UZI::log($client);
			return $r;
		} catch (Exception $e){
			Utils::log($e);
			UZI::log($client);
			return null;
		}
	}
	
	static public function restClient(){	
		$client = new Zend_Rest_Client("http://localhost/rest");
		$result =  $client->upAC(222,'yyy')->get();// post, put,delete ???
		//$client->method1("zz");$client->method2(21);$client->get();
		// if($result->isSuccess())
		echo $result;// echo $result->firstname()."ssss";
	}
	
	static public function xmlrpcClient(){
		$client = new Zend_XmlRpc_Client('http://localhost/xmlrpc');
		//$client->setSkipSystemLookup();// przy castowniu NA TYPY XMLRPC - żeby nie było problemu
		
		// $param = new Zend_XmlRpc_Value_DateTime($date->get(Zend_Date::ISO_8601));
		// Zend_XmlRpc_Value::getXmlRpcValue($param, null = AUTODETECT lub Zend_XmlRpc_Value::...);
		
		//$client->getIntrospector();
		// $client->getHttpClient(); $client->setHttpClient();// do uzycia z Zend_Http_Client_Adapter_Test
		try {
			echo $client->call('upAC',array(333,'zzz'));// service namespace
			// lub
			//$service = $client->getProxy();// lub getProxy('namespace');
			// echo $service->upAC(333,'zzz'); // j.w.
			// lub
			// $request = new Zend_XmlRpc_Request();
			// $request->setMethod('upAC');$resuest->setParams(array(333,'zzz'));
			// $response = $client->doRequest($request);
			// $response->isFault(); 
			// + dla obu
			// echo $client->getLastResponse(); // to samo chyba
			// $client->getLastRequest();
		} catch (Zend_XmlRpc_Client_HttpException $e) {
			echo $e->getCode();
			echo $e->getMessage();
		} catch (Zend_XmlRpc_Client_FaultException $e) {
			echo $e->getCode();
			echo $e->getMessage();
		}
		/**
		 * TODO do klasy webserisu - niepamietam po co to.
		 */
		// private $flag;
		// /** @param boolean $flag */
		// public function _construct($flag = false){$this->flag = $flag;}
	}
	
	
	/**
	 * UPDATE - developerski
	 */
	static public function updateActionCodes(){
		$db = Zend_Db_Table::getDefaultAdapter();
    	$sel = $db->select()
    		->from(array('a'=>'action'),array('action_id'))
    		->join(array('p'=>'project'),'p.project_id = a.project_id',array('project_account_code'))
    		->where('p.project_is_container = 0')->order(array('action_id ASC'));
    	$q = $db->query($sel);
    	$action = new Action();
    	$action->update(array('action_code'=>null,'action_code_granted'=>null),'1=1');
    	while ($a = $q->fetch(Zend_Db::FETCH_OBJ)){
    		$new_code = "$a->project_account_code-";
    		$last_act = $action->fetchAll(Utils::quote('action_code LIKE ?',$new_code.'%'), 'action_id DESC', 1)->current();
    		if(!is_object($last_act)) $autoinc = 1;
    		else {
    			$autoinc = intval(substr($last_act->action_code, strripos($last_act->action_code,'-')+1))+1;
    		}
    		$db->update('action', array('action_code'=>$new_code.$autoinc,'action_code_granted'=>date('c')), "action_id = $a->action_id");
 	  	}
	}
	
	/**
	 * EXAMPLE php closure
	 */
	static public function closure(){
 		function with_closure($item,$fn=null){
 			if($fn == null){
				$fn = function ($was){
		 			return $was;		
		 		};
			}
	 		return $fn($item);
 		}
		$var = 80;
		echo with_closure('aaa',function($was)use ($var){return 'nie '.$was.' '.$var;});
	}
	
	/**
	 * @deprecated - bo jest autoload i wali exception;
	 */
	static public function classIfIs($className,$extends){// TODO czy da sie bez instancjonowania klasy?
		if(@class_exists($className)){
			$class = new $className();
			if($class instanceof $extends){
				return $class;
			}
		}
		return false;
	}
	
	/*
	static public function rq($params){
		$r = Zend_Controller_Front::getInstance()->getRequest();
		$a = array();
		foreach ($params as $param){
			//array_shift(debug_backtrace());
			array_push($a,$r->getParam($param));
		}
		return $a;
	}*/
	
	
}

